package com.zimi.core.bean;

import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.HashMap;
import java.util.Map;

public class AjaxValue {

    private static final String STATUS_CODE_SUCCEEDED = "200";
    private static final String STATUS_CODE_FAILED = "500";
    private static final String DEFAULT_VALUE_SUCCEEDED = "success";
    private static final String DEFAULT_VALUE_FAILED = "failed";

    /**
     * 执行成功
     *
     * @param data 数据
     * @return ap
     */
    public static Map<String, Object> success(Object data) {
        return result(true, data, DEFAULT_VALUE_SUCCEEDED);
    }

    /**
     * 执行成功(带信息参数)
     *
     * @param data 数据
     * @param msg  消息
     * @return Map
     */
    public static Map<String, Object> success(Object data, String msg) {
        return result(true, data, msg);
    }

    /**
     * 执行失败
     *
     * @param data 数据
     * @return ap
     */
    public static Map<String, Object> fail(Object data) {
        return result(false, data, DEFAULT_VALUE_FAILED);
    }

    /**
     * 执行失败(带信息参数)
     *
     * @param data 数据
     * @param msg  消息
     * @return Map
     */
    public static Map<String, Object> fail(Object data, String msg) {
        return result(true, data, msg);
    }

    /**
     * 分页返回结果
     *
     * @param page 分页信息
     * @return Map
     */
    public static Map<String, Object> page(IPage page) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", "0");
        result.put("data", page.getRecords());
        result.put("count", page.getTotal());
        return result;
    }

    /**
     * @param flag 是否成功
     * @param data 数据
     * @param msg  消息
     * @return 结果
     */
    private static Map<String, Object> result(boolean flag, Object data, String msg) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", flag ? STATUS_CODE_SUCCEEDED : STATUS_CODE_FAILED);
        result.put("data", data);
        result.put("msg", msg);
        return result;
    }


}
